var headMenu = {
        data: [
            {text: '相册管理', id: 'ablum_manager', cls: 'menu_title', icon: '/static/images/ablum_icon.png', children: [
                {text: '相册列表', id: 'ablum_list', cls: 'menu_children', url: '/a/album/'},
                {text: '新建相册', id: 'add_ablum', cls: 'menu_children', url: '/a/album/add/'}
            ]},
            {text: '图片上传', id: 'upload', cls: 'menu_title', icon: '/static/images/upload_icon.png', children: [
                {text: '普通上传', id: 'normal_upload', cls: 'menu_children', url: '/a/upload/'},
                {text: 'Html5上传', id: 'html5_upload', cls: 'menu_children', url: '/a/upload5/'},
                {text: 'URL上传', id: 'url_upload', cls: 'menu_children', url: '/a/g/'},
                {text: '系统图片上传', id: 'sys_upload', cls: 'menu_children', url: '/a/uploadsys/'}
            ]},
            {text: '图片采集', id: 'pick', cls: 'menu_title', icon: '/static/images/pick_icon.png', children: [
                {text: '采集列表', id: 'pick_list', cls: 'menu_children', url: '/a/pick/'},
                {text: '新建采集', id: 'pick_add', cls: 'menu_children', url: '/a/pick/add/'}
            ]},
            {text: '系统设置', id: 'setting', cls: 'menu_title', icon: '/static/images/setting_icon.png', children: [
                {text: '系统设置', id: 'settingc', cls: 'menu_children', url: '/a/settings/'}
            ]},
            {text: '防盗链设置', id: 'antileech', cls: 'menu_title', icon: '/static/images/antipick_icon.png', children: [
                {text: '防盗链设置', id: 'antileechc', cls: 'menu_children', url: '/a/antileech/'}
            ]},
            {text: '一键复制', id: 'copy', cls: 'menu_title', icon: '/static/images/copy_icon.png', children: [
                {text: '一键复制', id: 'copyc', cls: 'menu_children', url: '/a/copy/'}
            ]},
            {text: 'API设置', id: 'api', cls: 'menu_title', icon: '/static/images/api_icon.png', children: [
                {text: 'API设置', id: 'apic', cls: 'menu_children', url: '/a/api/'}
            ]}
        ],
        target: '.menu'
    };

    (function(h){
        h.create = function(){
            if(typeof h.data != 'undefined'){
                h.createDom(h.data, $(h.target));
            }
        };

        h.createDom = function(data, target, rootId){
            var html = "";
            if(typeof data != 'undefined'){
                var display = '';
                var status = 0;
                if(typeof rootId != 'undefined' && rootId != h.initMenu){
                    display = 'style="display:none;"';
                    status = 1;
                }

                for(var i=0; i < data.length;i++){
                    if( data[i].hasOwnProperty('children') && typeof data[i].children != 'undefined' ){
                        var rootDom = $('<div></div>');
                        if( data[i].hasOwnProperty('icon') && typeof data[i].icon != 'undefined' ){
                            var iconDom = $('<span></span>');
                            iconDom.addClass("icon");
                            iconDom.html('<img src="' +data[i].icon+ '" border="0" alt=""/>');
                            iconDom.appendTo(rootDom);
                        }
                        $('<span>' +data[i].text+ '</span>').appendTo(rootDom);
                        if(h.hasOwnProperty('status_icon') && typeof h.status_icon != 'undefined'){
                            var iconDom = $('<span></span>');
                            iconDom.addClass("menu_status");
                            iconDom.html('<img src="' +h.status_icon[status]+ '" border="0" alt=""/>');
                            iconDom.appendTo(rootDom);
                        }
                        if( data[i].hasOwnProperty('cls') && typeof data[i].cls != 'undefined' ){
                            rootDom.addClass(data[i].cls);
                        }
                        var divDom = $('<div></div>');
                        divDom.attr('id', data[i].id);
                        divDom.attr('name', data[i].id);
                        if(data[i].children.length > 0){
                            var childDom = h.createDom(data[i].children, divDom, data[i].id);
                            divDom.html(childDom);
                        }
                        
                        if( data[i].hasOwnProperty('url') && typeof data[i].url != 'undefined' ){
                            h.bindEvent(rootDom, data[i].url);
                        }
                        else{
                            h.bindEvent(rootDom);
                        }
                        
                        divDom.prepend(rootDom);
                        target.append(divDom);
                    }
                    else{
                        if( data[i].hasOwnProperty('url') && typeof data[i].url != 'undefined' ){
                            html += '<a href=' + data[i].url + ' id="' +data[i].id+ '" class="' +data[i].cls+ '" '+ display +'>' + data[i].text + '</a>';
                        }
                        else{
                            html += '<a href=' + data[i].url + ' id="' +data[i].id+ '" '+ display +'>' + data[i].text + '</a>';
                        }
                    }
                }
            }

            return html;
        };

        h.bindEvent = function(obj, url){
            obj.bind('click', function(){
                if(typeof url != 'undefined'){
                    window.location.href = url;
                }
                else{
                    obj.nextAll().each(function(){
                        if($(this).css("display") == "none"){
                            $(this).css("display", "");
                        }
                        else{
                            $(this).css("display", "none");
                        }
                    });
                }
            });
        };
    })(headMenu);